<template>
  <div class="CountBox">
    <div class="frame">
      <button class="minus" @click="handleSub">-</button>
      <input :value="value" class="inp" type="text" @change="handleChange" />
      <button class="add" @click="handleAdd">+</button>
    </div>
  </div>
</template>
<script>
export default {
  props: {
    value: {
      type: Number,
      default: 1
    }
  },
  components: {},
  data () {
    return {}
  },
  computed: {},
  methods: {
    handleSub () {
      if (this.value <= 1) {
        return
      }
      this.$emit('input', this.value - 1)
    },
    handleAdd () {
      this.$emit('input', this.value + 1)
    },
    handleChange (e) {
      const num = +e.target.value // 转数字处理
      if (isNaN(num) || num < 1) {
        e.target.value = this.value
        return
      }
      this.$emit('input', num)
    }
  },
  created () {}
}
</script>
<style lang="less" scoped>
.CountBox {
  .frame {
    width: 110px;
    display: flex;

    .add,
    .minus {
      width: 30px;
      height: 30px;
      outline: none;
      border: none;
      background-color: #efefef;
    }

    .inp {
      width: 40px;
      height: 30px;
      outline: none;
      border: none;
      margin: 0 5px;
      text-align: center;
      background-color: #efefef;
    }
  }
}
</style>
